/**
 * 
 */
package com.yidu.springbootdemo.permission.service;

import com.github.pagehelper.PageHelper;
import com.yidu.springbootdemo.permission.action.vo.TcharacterVo;
import com.yidu.springbootdemo.permission.dao.TcharacterMapper;
import com.yidu.springbootdemo.permission.domain.Tcharacter;
import com.yidu.springbootdemo.permission.domain.TroleUser;
import com.yidu.springbootdemo.permission.domain.TrolesModules;
import com.yidu.springbootdemo.permission.tools.Pages;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * 类的描述： 角色service接口实现类
 * 
 * @author   张韦超
 * @date 2019年9月25日 下午3:25:11
 */
@Service
public class TcharacterServiceImp implements TcharacterService {
	@Resource
	TcharacterMapper tchacterMapper;
	
	@Resource
	TrolesModulesService trolesModulesService;

	@Resource
	TroleUserService troleUserService;

	@Override
	public void add(Tcharacter tc) {
		//调用增加角色的方法
		tchacterMapper.insertSelective(tc);
		//循环查 tc中的模块id
		for (int i = 0; i < tc.getChModule().length; i++) {
			//得到一个UUID的字符串
			String rd = UUID.randomUUID().toString().replaceAll("-", "");
			TrolesModules tr = new TrolesModules();//new 一个角色模块对象
			tr.setRolId(rd);//将 uuid 放入到对象的id中
			tr.setChaId(tc.getChaId());//角色id也放入
			tr.setModId(tc.getChModule()[i]);//分别将模块id也放入到对象中
			trolesModulesService.add(tr);//调用 添加角色模块的方法
		}
		TroleUser tro = new TroleUser();
		tro.setChaId(tc.getChaId());
		tro.setChaId2(tc.getChaId2());
		troleUserService.update(tro);//修改角色用户表 将原id 替换成新id
	}

	@Override
	public void delete(String chaID) {
	    //new一个角色对象
		Tcharacter tch = new Tcharacter();
		//将id放入到到对象中
		tch.setChaId(chaID);
		//将 角色中setcha 设置为N 不可用
		tch.setChaState("N");
		tchacterMapper.updateByPrimaryKeySelective(tch);
	}

	@Override
	public void update(String chaID) {
		tchacterMapper.deleteByPrimaryKey(chaID);
	}

	@Override
	public List<TcharacterVo> findAll(TcharacterVo tch, Pages pages1) {
		//分页插件 PageHelper off 参数 开始行数  limit
		PageHelper.offsetPage(pages1.getStartRows(), pages1.getLimit());
		//得到新的 角色对象
		Tcharacter tc = new Tcharacter();
		//转换
		BeanUtils.copyProperties(tch, tc);
		//得到 新的map集合
		Map<String, Object> map = new HashMap<String, Object>();
        //将 角色对象 和 分页 参数加入到map集合中
		map.put("tch", tc);
        map.put("pages", pages1);
		List<Tcharacter> list = tchacterMapper.findAll(map);
		List<TcharacterVo> list2 = new ArrayList<TcharacterVo>();
		//循环list集合 将他加入到list2集合中 BeanUtils.copy
		for (Iterator iterator = list.iterator(); iterator.hasNext();) {
			Tcharacter tcharacter = (Tcharacter) iterator.next();
			TcharacterVo vo = new TcharacterVo();
			BeanUtils.copyProperties(tcharacter, vo);
			list2.add(vo);
		}
		//开启分页插件 start
		PageHelper.startPage(pages1.getStartRows(), pages1.getLimit());
		return list2;
	}

	@Override
	public TcharacterVo findByID(String chaID) {
		Tcharacter tc = tchacterMapper.selectByPrimaryKey(chaID);
		TcharacterVo vo = new TcharacterVo();
		BeanUtils.copyProperties(tc, vo);
		return vo;
	}

	@Override
	public List<TcharacterVo> findAll2() {
		List<Tcharacter> list = tchacterMapper.findAll2();
		List<TcharacterVo> list2 = new ArrayList<TcharacterVo>();
		for (Iterator iterator = list.iterator(); iterator.hasNext();) {
			Tcharacter tcharacter = (Tcharacter) iterator.next();
			TcharacterVo vo = new TcharacterVo();
			BeanUtils.copyProperties(tcharacter, vo);
			list2.add(vo);
		}
		return list2;
	}

	@Override
	public int count(TcharacterVo tch) {
		// TODO Auto-generated method stub
		Tcharacter tc = new Tcharacter();
		BeanUtils.copyProperties(tch, tc);
		return tchacterMapper.count(tc);
	}

}
